package yl.hs.bmipfull.service.laboratorydata.imple;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmInspectionReportDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmInspectionReportEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmStoneReportItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabStoneInspectionItemsEntity;
import yl.hs.bmipfull.pojo.material.RawMaterialStorageRecordEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabRmInspectionReportService;
import yl.hs.bmipfull.service.laboratorydata.ILabRmStoneReportItemService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 原材检测报告——基本信息服务层接口实现
 * @since 2022年06月24日 09时27分44秒 <br/>
 */
@Service
public class LabRmInspectionReportServiceImpl extends LaboratorydataBaseService implements ILabRmInspectionReportService {

    @Autowired
    ILabRmInspectionReportDao dao;
    @Autowired
    ILabRmStoneReportItemService reportItemService;

    @Override
    public List<LabRmInspectionReportEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabRmInspectionReportEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<LabRmInspectionReportEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabRmInspectionReportEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabRmInspectionReportEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public LabRmInspectionReportEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabRmInspectionReport(Map<String, Object> model, String sn) throws Exception {
        var user= HsApp.getCurrent();
        Integer result = 0;
        LabRmInspectionReportEntity entity = null;
        ObjectMapper objectMapper = new ObjectMapper();
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabRmInspectionReportEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabRmInspectionReportEntity.class));
            entity.setCheckerSN(user.getUserSN());
            entity.setCheckerName(user.getUserName());
            var subData=model.get("subData");
            var graveDataEntity=objectMapper.convertValue(subData, LabRmStoneReportItemEntity.class);
            graveDataEntity.setReportSN(entity.getSn());
            result = dao.insertNew(entity);
            reportItemService.saveLabRmStoneReportItemOnPojo(graveDataEntity, "");
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入原材检测报告——基本信息信息", Tl.toJson(entity), LabRmInspectionReportEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            reportItemService.deleteByReportSN(sn);
            var graveData=model.get("subData");
            var graveDataEntity=objectMapper.convertValue(graveData, LabRmStoneReportItemEntity.class);
            graveDataEntity.setReportSN(entity.getSn());
            reportItemService.saveLabRmStoneReportItemOnPojo(graveDataEntity, "");
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新原材检测报告——基本信息信息", Tl.toJson(entity), LabRmInspectionReportEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabRmInspectionReportEntity.class);
        }
        return result;
    }

    @Override
    public List<LabRmInspectionReportEntity> queryByRmSN(String rmSN) {
        var rminspectionreport = dao.queryByRmSN(rmSN);

        return null;
    }
}